package com.study.cloud.gateway.client.support.dubbo;

import com.study.cloud.gateway.client.core.ApiAnnotationScanner;
import com.study.cloud.gateway.client.core.ApiProperties;
import com.study.cloud.gateway.client.support.AbstractClientRegisterManager;
import com.study.cloud.gateway.common.constants.BasicConst;
import com.study.cloud.gateway.common.constants.GatewayConst;
import com.study.cloud.gateway.common.rule.ServiceDefinition;
import com.study.cloud.gateway.common.rule.ServiceInstance;
import com.study.cloud.gateway.common.util.NetUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.spring.ServiceBean;
import org.apache.dubbo.config.spring.context.event.ServiceBeanExportedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import java.util.HashSet;
import java.util.Set;

/**
 * @author wangwancheng
 * @create 2025/7/19 19:51
 */
@Slf4j
public class DubboClientRegisterManager extends AbstractClientRegisterManager implements ApplicationListener<ApplicationEvent> {

    Set<Object> set = new HashSet<>();

    public DubboClientRegisterManager(ApiProperties apiProperties) {
        super(apiProperties);
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        //serviceBean 生成事件
        if (event instanceof ServiceBeanExportedEvent){
            ServiceBean serviceBean = ((ServiceBeanExportedEvent) event).getServiceBean();
            try {
                doRegisterDubbo(serviceBean);
            }catch (Exception e){
                log.error("dubbo register error",e);
                throw new  RuntimeException(e);
            }
        } else if (event instanceof ApplicationStartedEvent) {
            log.info("dubbo register started");
        }

    }

    private void doRegisterDubbo(ServiceBean serviceBean) {

        Object bean = serviceBean.getRef();
        if (set.contains( bean)){
            return;
        }
        //服务定义信息
        ServiceDefinition serviceDefinition = ApiAnnotationScanner.getInstance().scanner(bean,serviceBean);
        if (serviceDefinition == null){
            return;
        }
        serviceDefinition.setEnvType(getApiProperties().getEnv());

        //服务实例
        ServiceInstance serviceInstance = new ServiceInstance();
        String localIp = NetUtils.getLocalIp();
        Integer port  = serviceBean.getProtocol().getPort();
        String serviceInstanceId = localIp + BasicConst.COLON_SEPARATOR + port;
        String uniqueId = serviceDefinition.getUniqueId();
        String version = serviceDefinition.getVersion();
        serviceInstance.setServiceInstanceId(serviceInstanceId);
        serviceInstance.setUniqueId(uniqueId);
        serviceInstance.setIp(localIp);
        serviceInstance.setPort(port);
        serviceInstance.setRegisterTime(System.currentTimeMillis());
        serviceInstance.setVersion( version);
        serviceInstance.setEnabled(true);
        if (getApiProperties().isGray()){
            serviceInstance.setGray(true);
        }
        serviceInstance.setWeight(GatewayConst.DEFAULT_WEIGHT);
        register(serviceDefinition, serviceInstance);
        set.add(bean);

    }
}
